﻿namespace Hidistro.UI.Web.Admin
{
    using Hidistro.ControlPanel.Commodities;
    using Hidistro.ControlPanel.Store;
    using Hidistro.Core;
    using Hidistro.Entities.Commodities;
    using Hidistro.Entities.Store;
    using Hidistro.UI.Common.Controls;
    using Hidistro.UI.ControlPanel.utility;
    using Hishop.Components.Validation;
    using kindeditor.Net;
    using System;
    using System.Collections.Generic;
    using System.Web.UI.WebControls;

    [PrivilegeCheck(Privilege.BrandCategories)]
    public class EditBrandCategory : AdminPage
    {
        private int brandId;
        protected ImageLinkButton btnDeleteLogo;
        protected ImageLinkButton btnDeleteXx;
        protected ImageLinkButton btnDeleteSq;
        protected Button btnUpdateBrandCategory;
        protected Button btnUpoad;
        protected Button Btn_Sq;
        protected Button Btn_Xx;
        protected ProductTypesCheckBoxList chlistProductTypes;
        protected KindeditorControl fckDescription;
        protected FileUpload fileUpload;
        protected FileUpload fileUpload_Xx;
        protected FileUpload fileUpload_Sq;
        protected HiImage imgLogo;
        protected HiImage ImgXx;
        protected HiImage ImgSq;
        protected TextBox txtBrandName;
        protected TextBox txtCompanyUrl;
        protected TextBox txtkeyword;
        protected TextBox txtMetaDescription;
        protected TextBox txtReUrl;


        #region  删除事件
        private void btnDeleteLogo_Click(object sender, EventArgs e)
        {
            BrandCategoryInfo brandCategoryInfo = this.GetBrandCategoryInfo();
            try
            {
                ResourcesHelper.DeleteImage(brandCategoryInfo.Logo);
                brandCategoryInfo.Logo = null;
                this.ViewState["Logo"] = null;
                CatalogHelper.UpdateBrandCategory(brandCategoryInfo);
            }
            catch
            {
                this.ShowMsg("删除失败", false);
                return;
            }
            this.loadData();
        }
        private void btnDeleteXx_Click(object sender, EventArgs e)
        {
            BrandCategoryInfo brandCategoryInfo = this.GetBrandCategoryInfo();
            try
            {
                ResourcesHelper.DeleteImage(brandCategoryInfo.BrandXx);
                brandCategoryInfo.BrandXx = null;
                this.ViewState["BrandXx"] = null;
                CatalogHelper.UpdateBrandCategory(brandCategoryInfo);
            }
            catch
            {
                this.ShowMsg("删除失败", false);
                return;
            }
            this.loadData();
        }
        private void btnDeleteSq_Click(object sender, EventArgs e)
        {
            BrandCategoryInfo brandCategoryInfo = this.GetBrandCategoryInfo();
            try
            {
                ResourcesHelper.DeleteImage(brandCategoryInfo.BrandSq);
                brandCategoryInfo.BrandSq = null;
                this.ViewState["BrandSq"] = null;
                CatalogHelper.UpdateBrandCategory(brandCategoryInfo);
            }
            catch
            {
                this.ShowMsg("删除失败", false);
                return;
            }
            this.loadData();
        }

        #endregion

        protected void btnUpdateBrandCategory_Click(object sender, EventArgs e)
        {
            BrandCategoryInfo brandCategoryInfo = this.GetBrandCategoryInfo();
            if (string.IsNullOrEmpty(brandCategoryInfo.Logo))
            {
                this.ShowMsg("请上传一张品牌LOGO图片", false);
            }
            else if (this.ValidationBrandCategory(brandCategoryInfo))
            {
                if (CatalogHelper.UpdateBrandCategory(brandCategoryInfo))
                {
                    base.Response.Redirect(Globals.GetAdminAbsolutePath("/product/BrandCategories.aspx"), true);
                }
                else
                {
                    this.ShowMsg("编辑品牌分类失败", true);
                }
            }
        }

        #region 更新事件
        private void btnUpoad_Click(object sender, EventArgs e)
        {
            BrandCategoryInfo brandCategoryInfo = this.GetBrandCategoryInfo();
            if (this.fileUpload.HasFile)
            {
                try
                {
                    ResourcesHelper.DeleteImage(brandCategoryInfo.Logo);
                    brandCategoryInfo.Logo = CatalogHelper.UploadBrandCategorieImage(this.fileUpload.PostedFile);
                    this.ViewState["Logo"] = brandCategoryInfo.Logo;
                }
                catch
                {
                    this.ShowMsg("图片上传失败，您选择的不是图片类型的文件，或者网站的虚拟目录没有写入文件的权限", false);
                    return;
                }
                CatalogHelper.UpdateBrandCategory(brandCategoryInfo);
            }
            this.loadData();
        }
        private void btn_Sq_Click(object sender, EventArgs e)
        {
            BrandCategoryInfo brandCategoryInfo = this.GetBrandCategoryInfo();
            if (this.fileUpload_Sq.HasFile)
            {
                try
                {
                    ResourcesHelper.DeleteImage(brandCategoryInfo.BrandSq);
                    brandCategoryInfo.BrandSq = CatalogHelper.UploadBrandCategorieImage(this.fileUpload_Sq.PostedFile);
                    this.ViewState["BrandSq"] = brandCategoryInfo.BrandSq;
                }
                catch
                {
                    this.ShowMsg("图片上传失败，您选择的不是图片类型的文件，或者网站的虚拟目录没有写入文件的权限", false);
                    return;
                }
                CatalogHelper.UpdateBrandCategory(brandCategoryInfo);
            }
            this.loadData();
        }
        private void btn_Xx_Click(object sender, EventArgs e)
        {
            BrandCategoryInfo brandCategoryInfo = this.GetBrandCategoryInfo();
            if (this.fileUpload_Xx.HasFile)
            {
                try
                {
                    ResourcesHelper.DeleteImage(brandCategoryInfo.BrandXx);
                    brandCategoryInfo.BrandXx = CatalogHelper.UploadBrandCategorieImage(this.fileUpload_Xx.PostedFile);
                    this.ViewState["BrandXx"] = brandCategoryInfo.BrandXx;
                }
                catch
                {
                    this.ShowMsg("图片上传失败，您选择的不是图片类型的文件，或者网站的虚拟目录没有写入文件的权限", false);
                    return;
                }
                CatalogHelper.UpdateBrandCategory(brandCategoryInfo);
            }
            this.loadData();
        }

        #endregion

        private BrandCategoryInfo GetBrandCategoryInfo()
        {
            BrandCategoryInfo info = new BrandCategoryInfo
            {
                BrandId = this.brandId
            };
            if (this.ViewState["Logo"] != null)
            {
                info.Logo = (string)this.ViewState["Logo"];
            }
            if (this.ViewState["BrandSq"] != null)
            {
                info.BrandSq = (string)this.ViewState["BrandSq"];
            }
            if (this.ViewState["BrandXx"] != null)
            {
                info.BrandXx = (string)this.ViewState["BrandXx"];
            }
            info.BrandName = Globals.HtmlEncode(this.txtBrandName.Text.Trim());
            if (!string.IsNullOrEmpty(this.txtCompanyUrl.Text))
            {
                info.CompanyUrl = this.txtCompanyUrl.Text.Trim();
            }
            else
            {
                info.CompanyUrl = null;
            }
            info.RewriteName = Globals.HtmlEncode(this.txtReUrl.Text.Trim());
            info.MetaKeywords = Globals.HtmlEncode(this.txtkeyword.Text.Trim());
            info.MetaDescription = Globals.HtmlEncode(this.txtMetaDescription.Text.Trim());
            info.Description = (!string.IsNullOrEmpty(this.fckDescription.Text) && (this.fckDescription.Text.Length > 0)) ? this.fckDescription.Text : null;
            IList<int> list = new List<int>();
            foreach (ListItem item in this.chlistProductTypes.Items)
            {
                if (item.Selected)
                {
                    list.Add(int.Parse(item.Value));
                }
            }
            info.ProductTypes = list;
            return info;
        }

        private void loadData()
        {
            BrandCategoryInfo brandCategory = CatalogHelper.GetBrandCategory(this.brandId);
            if (brandCategory == null)
            {
                base.GotoResourceNotFound();
            }
            else
            {
                this.ViewState["Logo"] = brandCategory.Logo;
                foreach (ListItem item in this.chlistProductTypes.Items)
                {
                    if (brandCategory.ProductTypes.Contains(int.Parse(item.Value)))
                    {
                        item.Selected = true;
                    }
                }
                this.txtBrandName.Text = Globals.HtmlDecode(brandCategory.BrandName);
                this.txtCompanyUrl.Text = brandCategory.CompanyUrl;
                this.txtReUrl.Text = Globals.HtmlDecode(brandCategory.RewriteName);
                this.txtkeyword.Text = Globals.HtmlDecode(brandCategory.MetaKeywords);
                this.txtMetaDescription.Text = Globals.HtmlDecode(brandCategory.MetaDescription);
                this.fckDescription.Text = brandCategory.Description;
                if (string.IsNullOrEmpty(brandCategory.Logo))
                {
                    this.btnDeleteLogo.Visible = false;
                }
                else
                {
                    this.btnDeleteLogo.Visible = true;
                }
                if (string.IsNullOrEmpty(brandCategory.BrandXx))
                {
                    this.btnDeleteXx.Visible = false;
                }
                else
                {
                    this.btnDeleteXx.Visible = true;
                }
                if (string.IsNullOrEmpty(brandCategory.BrandSq))
                {
                    this.btnDeleteSq.Visible = false;
                }
                else
                {
                    this.btnDeleteSq.Visible = true;
                }
                this.imgLogo.ImageUrl = brandCategory.Logo;
                this.ImgXx.ImageUrl = brandCategory.BrandXx;
                this.ImgSq.ImageUrl = brandCategory.BrandSq;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!int.TryParse(this.Page.Request.QueryString["brandId"], out this.brandId))
            {
                base.GotoResourceNotFound();
            }
            else
            {
                this.btnUpdateBrandCategory.Click += new EventHandler(this.btnUpdateBrandCategory_Click);
                this.btnUpoad.Click += new EventHandler(this.btnUpoad_Click);
                this.Btn_Sq.Click += new EventHandler(this.btn_Sq_Click);
                this.Btn_Xx.Click += new EventHandler(this.btn_Xx_Click);
                this.btnDeleteLogo.Click += new EventHandler(this.btnDeleteLogo_Click);
                this.btnDeleteXx.Click += new EventHandler(this.btnDeleteXx_Click);
                this.btnDeleteSq.Click += new EventHandler(this.btnDeleteSq_Click);
                if (!this.Page.IsPostBack)
                {
                    this.chlistProductTypes.DataBind();
                    this.loadData();
                }
            }
        }

        private bool ValidationBrandCategory(BrandCategoryInfo brandCategory)
        {
            ValidationResults results = Hishop.Components.Validation.Validation.Validate<BrandCategoryInfo>(brandCategory, new string[] { "ValBrandCategory" });
            string msg = string.Empty;
            if (!results.IsValid)
            {
                foreach (ValidationResult result in (IEnumerable<ValidationResult>)results)
                {
                    msg = msg + Formatter.FormatErrorMessage(result.Message);
                }
                this.ShowMsg(msg, false);
            }
            return results.IsValid;
        }
    }
}

